package com.kl.drs.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.kl.drs.bean.bo.TaskInfoBO;
import com.kl.drs.dao.TaskInfoDAO;
import com.kl.drs.service.RecordingAnalysisService;
import com.kl.drs.util.ConfigsUtil;
import com.kl.drs.util.Log4jUtils;

/**
 * 解析数据业务
 * @author WYN
 */
@Service("recordingAnalysisService")
public class RecordingAnalysisServiceImpl implements RecordingAnalysisService{

	@Autowired 
	private TaskInfoDAO taskInfoDAO;
	
	/**
	 * 插入任务
	 */
	@Override
	public int createOrUpdateRecording(TaskInfoBO taskInfo) throws IOException {
		TaskInfoBO taskInfoBO= queryRecordTaskInfo(taskInfo);
		if(taskInfoBO!=null){
			return taskInfoDAO.updateTaskInfo(taskInfo); 
		}else{
			return taskInfoDAO.addTaskInfo(taskInfo); 
		}
	}
	/**
	 * 更新任务
	 */
	@Override
	public int updateRecording(TaskInfoBO taskInfo) throws IOException {
		return taskInfoDAO.updateTaskInfo(taskInfo); 
	}
	/**
	 * 文件上传
	 */
	@Override
	public Map fileUpload(MultipartFile uploadFile) throws IOException {
		Map result = new HashMap();
    	//资料上传
		//String relaPath=ConstatFinalUtil.CONFIG_JSON.getString("relaPath");
		//String savePath=ConstatFinalUtil.CONFIG_JSON.getString("truePath")+relaPath;
		String savePath=ConfigsUtil.getProperty("video.path", "");
		File file = new File(savePath);
		//判断上传文件的保存目录是否存在
		if (!file.exists() && !file.isDirectory()) {
		    Log4jUtils.logDebug(savePath+"目录不存在，需要创建");
		     file.mkdir();
		 }
		
		String filename = uploadFile.getOriginalFilename();
		
		InputStream in;
		FileOutputStream out;
		in = uploadFile.getInputStream();
		out = new FileOutputStream(savePath + File.separator + filename);
		byte buffer[] = new byte[1024];
		 int len = 0;
		 while((len=in.read(buffer))>0){
			 out.write(buffer, 0, len);
		 }
		 in.close();
		 out.close();
		 
		 String message = "文件上传成功！";
		 result.put("resultFlag", "SUCCESS");
		 result.put("message", message);
		 
		 return result; 
	}
	/**
	 * 读取任务详情
	 */
	@Override
	public TaskInfoBO queryRecordTaskInfo(TaskInfoBO taskInfo) {
		
		return taskInfoDAO.queryTaskInfo(taskInfo);
	}

}